from django.db.models import Q
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKU, GoodsCategory
from apps.meiduo_admin.serializers.sku import SKUModel, ThirdCategory, SKUidModel
from apps.meiduo_admin.utils.MeiduoPage import PageNum


class SKUView(ModelViewSet):

    pagination_class = PageNum
    # queryset = SKU.objects.all()
    serializer_class = SKUModel

    def get_queryset(self):

        key=self.request.query_params.get('keyword')

        print(key)
        if not key:
            return SKU.objects.all().order_by('-id')
        else:
            return SKU.objects.filter(Q(name__contains=key) | Q(caption__contains=key)).order_by('-id')


class ThirdCategoryView(ListAPIView):
    queryset =GoodsCategory.objects.filter(subs__isnull=True)
    serializer_class =ThirdCategory


class SKUid(ListAPIView):
    queryset = SKU.objects.all()
    serializer_class = SKUidModel

